package com.audaque.springboot.foshanupload.web.webdemo.controller;

import com.audaque.springboot.foshanupload.web.webdemo.model.domain.Person;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;

@Slf4j
@RestController
@RequestMapping("xml")
public class XmlController {

    @ResponseBody
    @RequestMapping("parse")
    public void parse() throws IOException {
        ClassPathResource classPathResource = new ClassPathResource("xml/ProcessBuilder.xml");
        InputStream inputStream = classPathResource.getInputStream();
        XMLDecoder xmlDecoder = new XMLDecoder(new BufferedInputStream(inputStream));
        Object result = null;
        try {
            result = xmlDecoder.readObject();
        } finally {
            xmlDecoder.close();
        }
    }

    @ResponseBody
    @RequestMapping("parse1")
    public void parse1() throws IOException {
        ClassPathResource classPathResource = new ClassPathResource("xml/ProcessBuilder1.xml");
        InputStream inputStream = classPathResource.getInputStream();
        XMLDecoder xmlDecoder = new XMLDecoder(new BufferedInputStream(inputStream));
        Object result = null;
        try {
            result = xmlDecoder.readObject();
        } finally {
            xmlDecoder.close();
        }
    }

    @ResponseBody
    @RequestMapping("serialize2")
    public void serialize2() throws IOException {
        ClassPathResource classPathResource = new ClassPathResource("xml/serialize1.xml");
        File file = new File(classPathResource.getURI());
        FileOutputStream fileOutputStream = new FileOutputStream(file);
        XMLEncoder xmlEncoder = new XMLEncoder(fileOutputStream);

        HashMap<Object, Object> map = new HashMap<>();
        map.put("123", "aaaa");
        map.put("321", new ArrayList<>());

        try {
            xmlEncoder.writeObject(map);
        } finally {
            xmlEncoder.close();
        }
    }

    @ResponseBody
    @RequestMapping("parse2")
    public void parse2() throws IOException {
        StringBufferInputStream stringBufferInputStream = new StringBufferInputStream(
                "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                        "<java version=\"11.0.16.1\" class=\"java.beans.XMLDecoder\">\n" +
                        " <object class=\"java.util.HashMap\">\n" +
                        "  <void method=\"put\">\n" +
                        "   <string>123</string>\n" +
                        "   <string>aaaa</string>\n" +
                        "  </void>\n" +
                        "  <void method=\"put\">\n" +
                        "   <string>321</string>\n" +
                        "   <object class=\"java.util.ArrayList\"/>\n" +
                        "  </void>\n" +
                        " </object>\n" +
                        "</java>\n");
        XMLDecoder xmlDecoder = new XMLDecoder(stringBufferInputStream);
        Object result = null;
        try {
            result = xmlDecoder.readObject();
        } finally {
            xmlDecoder.close();
        }
    }


    @ResponseBody
    @RequestMapping("serialize3")
    public void serialize3() throws IOException {
        ClassPathResource classPathResource = new ClassPathResource("xml/serialize3.xml");
        File file = new File(classPathResource.getURI());
        FileOutputStream fileOutputStream = new FileOutputStream(file);
        XMLEncoder xmlEncoder = new XMLEncoder(fileOutputStream);
        Person person = new Person();
        person.setId("1");
        person.setName("Ahmad");
        person.setFamily("Javadi");
        person.setSalary(4000);
        Person person1 = new Person();
        person1.setId("2");
        person1.setName("Gholam");
        person1.setFamily("Asadi");
        person1.setSalary(5000);

        try {
            xmlEncoder.writeObject(person);
            xmlEncoder.writeObject(person1);
        } finally {
            xmlEncoder.close();
        }

    }


    @ResponseBody
    @RequestMapping("parse3")
    public void parse3() throws IOException {
        StringBufferInputStream stringBufferInputStream = new StringBufferInputStream(
                "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                        "<java version=\"11.0.16.1\" class=\"java.beans.XMLDecoder\">\n" +
                        " <object class=\"com.audaque.springboot.foshanupload.web.webdemo.model.domain.Person\">\n" +
                        "  <void property=\"family\">\n" +
                        "   <string>Javadi</string>\n" +
                        "  </void>\n" +
                        "  <void property=\"id\">\n" +
                        "   <string>1</string>\n" +
                        "  </void>\n" +
                        "  <void property=\"name\">\n" +
                        "   <string>Ahmad</string>\n" +
                        "  </void>\n" +
                        "  <void property=\"salary\">\n" +
                        "   <long>4000</long>\n" +
                        "  </void>\n" +
                        " </object>\n" +
                        " <object class=\"com.audaque.springboot.foshanupload.web.webdemo.model.domain.Person\">\n" +
                        "  <void property=\"family\">\n" +
                        "   <string>Asadi</string>\n" +
                        "  </void>\n" +
                        "  <void property=\"id\">\n" +
                        "   <string>2</string>\n" +
                        "  </void>\n" +
                        "  <void property=\"name\">\n" +
                        "   <string>Gholam</string>\n" +
                        "  </void>\n" +
                        "  <void property=\"salary\">\n" +
                        "   <long>5000</long>\n" +
                        "  </void>\n" +
                        " </object>\n" +
                        "</java>\n");
        XMLDecoder xmlDecoder = new XMLDecoder(stringBufferInputStream);
        Person result = null;
        Person result1 = null;
        try {
            result = (Person) xmlDecoder.readObject();
            result1 = (Person) xmlDecoder.readObject();
        } finally {
            xmlDecoder.close();
        }

    }


}
